package com.jeomo.mem.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.constant.SearchParam;
import com.jeomo.common.core.constant.Sort;
import com.jeomo.common.core.constant.SystemConst;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.masterdata.starter.service.ILevelService;
import com.jeomo.mem.starter.entity.MemberDiscountRule;
import com.jeomo.mem.starter.mapper.MemberDiscountRuleMapper;
import com.jeomo.mem.starter.service.IMemberDiscountRuleService;
import com.jeomo.mem.starter.util.MemberUtil;
import com.jeomo.shiro.util.OrgUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:Jackpot
 * @create 2020-09-25 16:46
 * @description:
 */
@Service
@Transactional
public class MemberDiscountRuleServiceImpl  extends BaseServiceImpl<MemberDiscountRuleMapper, MemberDiscountRule>
        implements IMemberDiscountRuleService {

    private final String USE_LEVEL = "use_level";
    private final String USE_LEVELS = "use_levels";
    private final String USE_RANGE = "use_range";
    private final String START_FLAG = "start_flag";
    private final String CREATE_TIME = "create_time";
    private final String NAME = "name";

    @Resource
    private ILevelService levelService;

    @Override
    public PageResponseDto pageSearch(PageQueryDto dto){
        String currOrg = OrgUtil.getCurrOrg();
        //1. 处理查询参数
        Map<String, Object> searchParams = dto.getSearchParams();
        if(searchParams!=null){
            Map<String, Object> params = new HashMap<>(8);
            params.put(SearchParam.SEARCH_EQ + SystemConst.ORG, currOrg);
            params.put(SearchParam.SEARCH_LIKE+NAME,searchParams.get(NAME));
            params.put(SearchParam.SEARCH_EQ+USE_RANGE,searchParams.get(USE_RANGE));
            params.put(SearchParam.SEARCH_LIKE+USE_LEVELS,searchParams.get(USE_LEVEL));
            params.put(SearchParam.SEARCH_EQ+START_FLAG,searchParams.get(START_FLAG));
            dto.setSearchParams(params);
        }
        Map<String, String> sort = new HashMap<>(8);
        sort.put(CREATE_TIME, Sort.DESC);
        dto.setSorts(sort);

        PageResponseDto pageResponseDto = super.pageSearch(dto);
        //2. 处理返回结果
        Map<String, String> levelMap = levelService.queryCache();
        List<?> items = pageResponseDto.getItems();
        for (Object item : items) {
            MemberDiscountRule memberRule = (MemberDiscountRule)item;
            MemberUtil.assignName(memberRule,levelMap);
        }
        return pageResponseDto;
    }

    @Override
    public int updateRuleStatus(String ruleCode, boolean status) {
        MemberDiscountRule memberDiscountRule = new MemberDiscountRule();
        memberDiscountRule.setCode(ruleCode);
        memberDiscountRule.setStartFlag(status);
        boolean result = updateById(memberDiscountRule);
        return result==true?1:0;
    }
}
